
import { apiType, useApiFunc } from '@/pages/invoice/hooks';
import { Cascader } from '@formily/antd'
import { useEffect, useState } from 'react';
interface CascaderBizProps {
    value: any;
    onChange: any;
    api: apiType;
    fieldNames?: any;
    editable?: boolean;
}

export const CascaderBiz = (props: CascaderBizProps) => {
    const { value, api, onChange, ...extraProps } = props;
    const [options, setOptions] = useState<any[]>([])
    const [rq] = useApiFunc(api);
    useEffect(() => {
        rq({}).then((res: any) => {
            setOptions(res.result);
        })
    }, [JSON.stringify(api)])
    const filter = (inputValue: any, path: any) => {
        return path.some((option: any) => option[extraProps?.fieldNames?.label || 'label'].toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    }
    if (props?.editable === false) {
        return value?.join(',') || null
    }
    return <Cascader
        showSearch={{ filter }}
        options={options}
        value={value || []}
        onChange={onChange}
        {...extraProps}
    />
}